package appVeterinaria.servlets;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import appVeterinaria.negocio.GestionClientes;
import appVeterinaria.negocio.GestionFactura;
import appVeterinaria.modelo.Factura;
import appVeterinaria.modelo.Servicio;
import appVeterinaria.excepcion.*;

/**
 * Servlet implementation class FacturaInsertarServlet
 */
@WebServlet("/FacturaInsertarServlet")
public class FacturaInsertarServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public FacturaInsertarServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		String freg = request.getParameter("feReg");
		int idCita = Integer.parseInt(request.getParameter("idCita"));
		int s1 = Integer.parseInt(request.getParameter("s1"));
		int s2 = Integer.parseInt(request.getParameter("s2"));
		int s3 = Integer.parseInt(request.getParameter("s3"));
		
		
		int c1 = Integer.parseInt(request.getParameter("c1"));
		int c2 = Integer.parseInt(request.getParameter("c2"));
		int c3 = Integer.parseInt(request.getParameter("c3"));
	
		
		
		GestionFactura negocio = new GestionFactura();
			
		
		try {
			double p1 = negocio.obtenerCostoServicio(s1);
			double  p2 = negocio.obtenerCostoServicio(s2);
			double p3 = negocio.obtenerCostoServicio(s3);
			
			double mTotal=p1*c1+p2*c2+p3*c3;
			negocio.insertar(freg, mTotal);
			int lastId= negocio.obtenerUltimoId();
		    negocio.insertarCitaServicio(s1, idCita, c1, p1*c1, lastId);
			negocio.insertarCitaServicio(s2, idCita, c2, p2*c2, lastId);
			negocio.insertarCitaServicio(s3, idCita, c3, p3*c3, lastId);
			
			
			response.sendRedirect(request.getContextPath()	+ "/FacturaBuscarServlet?factura=1");
		} catch (DAOExcepcion e) {
			RequestDispatcher rd = request.getRequestDispatcher("error.jsp");
			rd.forward(request, response);
		}
	}

}

